<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")."GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/json");

require_once '../../../lib/php/vars.php';
require_once '../../../lib/php/dbCon.php';
require_once 'deleteFile.php';
	
//"prefix" gibt an was gelöscht werden soll
if(isset($_GET['prefix'])) {
	if (isset($_GET['id']) && isset($_GET['ctg_id']) && isset($_GET['path']) && isset($_GET['position'])) {
		
		$deleteID = $_GET['id'];
		$json = array('deleteID' => $deleteID);
		$ctgID = $_GET['ctg_id'];
		$json['ctg_id'] = $ctgID;
		$path = $documentRoot.$sitePath.$imageRoot.$_GET['path'];
		$json['path'] = $path;
		$prefix = $_GET['prefix'];
		$json['prefix'] = $prefix;
		//nur löschen wenn der Pfad auf Dateien oder Ordner
		//innerhalb des image Ordners zeigt
		if(stristr($path, $imageRoot) === FALSE) {
    		$json['error_files'] = 'invalid path: '.$path;
			$json['result_files'] = 0;
  		} else {
			if (file_exists($path)) {
				if(rmdirr($path)) {
					$json['result_files'] = 1;
				} else {
					$json['error_files'] = 'delete files failed'; 
					$json['result_files'] = 0;
				}
			} else {
				$json['error_files'] = 'no files associated';
				$json['result_files'] = 0;
			}
		}
		
		//Queries erstellen
		switch ($prefix) {
			case 'ctg':
				$queries = array('buttons' => 'DELETE FROM `'.$db.'`.`ak_buttons`'
									.' WHERE btn_category_id = '.$deleteID
									.' AND btn_project_id = 0',
								'projects' => 'DELETE FROM `'.$db.'`.`ak_projects`'
									.' WHERE prj_category_id = '.$deleteID,
								'categories' => 'DELETE FROM `'.$db.'`.`ak_categories`'
									.' WHERE ctg_id = '.$deleteID);
								
				break;
			case 'prj':
				$queries = array('buttons' => 'DELETE FROM `'.$db.'`.`ak_buttons`'
									.' WHERE btn_project_id = '.$deleteID,
								'projects' => 'DELETE FROM `'.$db.'`.`ak_projects`'
									.' WHERE prj_id = '.$deleteID);
				break;
			case 'btn':
				$queries = array('buttons' => 'DELETE FROM `'.$db.'`.`ak_buttons`'
									.' WHERE btn_id = '.$deleteID);
				break;
		}
		foreach($queries as $key => $query) {
			if($result = mysql_query($query)) {
		    	$json['result_'.$key] = 1;
		    	$json['result'] = 1;
		    } else {
		    	$json['error_'.$key] = 'delete '.$key.' failed: '.mysql_error();
		    	$json['error'] = 'delete failed: '.mysql_error();
		    	$json['result'] = 0;
		    }
		}
		
		
		//Position aktualisieren, so dass die Lücke durch das 
		//gelöschte item geschlossen wird
		if ($_GET['position'] != null) {
			$position = $_GET['position'];
			$json['position to delete'] = $position;
			//Name der Positions-Spalte
			$col_pos = $prefix.'_position';
			//Name der id Spalte
			$col_id = $prefix.'_id';
			//name der tabelle
			$table = '';
			switch ($prefix) {
				case 'ctg':
					$table = 'ak_categories';
					break;
				case 'prj':
					$table = 'ak_projects';
					break;
				case 'btn':
					$table = 'ak_buttons';
					break;
			}
			switch($prefix) {
				//Kategorien laden deren Position aktualisiert werden muss
				case 'ctg':
					$query = 'SELECT '.$col_id.', '.$col_pos
							.' FROM '.$db.'.'.$table.' WHERE '
							.$col_pos.' >= '.$position;
					break;
				//Projekte laden deren Position aktualisiert werden muss
				case 'prj':
					$query = 'SELECT '.$col_id.', '.$col_pos
							.' FROM '.$db.'.'.$table.' WHERE '
							.$col_pos.' >= '.$position
							.' AND '.$prefix.'_category_id = '.$ctgID;
					break;
			}
			//Ergebnis aufsteigend sortieren
			$query = $query.' ORDER BY '.$col_pos.' ASC';
			$json['query'] = $query;
			if($result = mysql_query($query)) {
				$i = 0;
				//alle Positionen um eins nach unten verschieben
				while($row = mysql_fetch_assoc($result)) {
					$query = 'UPDATE '.$db.'.'.$table.' SET '
							.$col_pos.' = '.($row[$col_pos]-1)
							.' WHERE '
							.$col_id.' = '.$row[$col_id];
					if ($resultUpadateQuery = mysql_query($query)) {
						$json['result'] = 1;
					} else {
						$json['result'] = 0;
						$json['error'] = 'query '.$query.' fehlgeschlagen';
					}
					$i++;
				}
			}
		}
	} else {
		$json = array('result' => 0,
						'error' => 'Parameter missing! ' 
									.'No "id" and/or no "path" submitted');
	}
} else {
	$json = array('result' => 0,
					'error' => 'Parameter missing! No type submitted!');
}
// Datenbankverbindung schließen
mysql_close($conn);
$encoded = json_encode($json);
echo $encoded;
unset($encoded);
?>